<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>编辑项目</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" autocomplete="off">
    <input type="hidden" name="id" value="{$project['id']}">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="{$project['name']}" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">详情描述</label>
        <div id="content" class="layui-input-block">
            {:htmlspecialchars_decode($project['content'])}
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/frontend/js/wangEditor.min.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = {
                'id': data.field.id,
                'name': data.field.name,
                'content': editor.txt.html(),
            };
            $.post('/frontend/project/update', postData, function (res) {
                if (res.code == 0) {
                    // 修改成功
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    },function () {
                        // 重新加载父页面
                        parent.location.reload();
                    })
                } else if (res.code == 302) {
                    // 错误跳转
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    // 修改失败
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

        // 自定义验证规则
        form.verify({
            name: function (value, item) {
                if (value.length > 20) {
                    return '标题不能超过20个字符';
                }
            }
        })

    });

    // 富文本编辑器
    const E = window.wangEditor;
    const editor = new E('#content');
    // 配置z-index
    editor.config.zIndex = 500;
    // 配置server接口地址
    editor.config.uploadImgServer = '/frontend/project/upload-img';
    editor.create();
</script>
</body>
</html>